package me.algo.tree;

import com.sun.source.tree.Tree;

import java.util.LinkedList;

/**
 * @author guozheng
 * @date 2024/5/2
 */
public class T14 {
    public static void main(String[] args) {

    }

    /**
     * 404. 左叶子之和
     * @param root
     * @return
     */
    public static int sumOfLeftLeaves(TreeNode root) {
        if (root == null) {
            return 0;
        }
        LinkedList<TreeNode> que = new LinkedList<>();
        que.addLast(root);
        int sum = 0;
        while (!que.isEmpty()) {
            int size = que.size();
            while (size > 0) {
                size--;
                TreeNode node = que.removeFirst();
                if (node.left != null) {
                    if (node.left.right == null && node.left.left == null) {
                        sum = sum + node.left.val;
                    }
                    que.addLast(node.left);
                }
                if (node.right != null) {
                    que.addLast(node.right);
                }
            }
        }
        return sum;
    }

    /**
     * LCR 175. 计算二叉树的深度
     * @param root
     * @return
     */
    public int calculateDepth(TreeNode root) {
        int dept = 0;
        if (root == null) {
            return dept;
        }
        LinkedList<TreeNode> que = new LinkedList<>();
        que.addLast(root);
        while (!que.isEmpty()) {
            int size = que.size();
            dept++;
            while (size > 0) {
                size--;
                TreeNode node = que.removeFirst();
                if (node.left != null) {
                    que.addLast(node.left);
                }
                if (node.right != null) {
                    que.addLast(node.right);
                }
            }
        }
        return dept;
    }

    /**
     * 2236. 判断根结点是否等于子结点之和
     * @param root
     * @return
     */
    public boolean checkTree(TreeNode root) {
        if (root == null) {
            return false;
        }
        if (root.left == null || root.right == null) {
            return false;
        }
        return root.val == root.left.val + root.right.val;
    }
}
